﻿using StudentCouncil.Model;
using StudentCouncil.Model.Models;
using StudentCouncil.Model.Parameter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StudentCouncil.DAL.DAO
{
    public class AssociationDAO
    {
        public static Pager<Association> GetListByPage(AssociationParameter parameter)
        {
            Pager<Association> result = new Pager<Association>();
            using (StudentContext db = new StudentContext())
            {
                var query = db.Association.AsQueryable();
                if (!string.IsNullOrEmpty(parameter.Assname))
                {
                    query = query.Where(t => t.Assname.Contains(parameter.Assname));
                }

                result.Total = query.Count();
                result.Rows = query.OrderBy(t => t.AssociationId)
                    .Skip(parameter.Skip)
                    .Take(parameter.PageSize)
                    .ToList();
                return result;
            }
        }
        public static Pager<Associationstatus> GetAssociationstatusListByPage(AssociationstatusParameter parameter)
        {
            Pager<Associationstatus> result = new Pager<Associationstatus>();
            using (StudentContext db = new StudentContext())
            {
                var query = db.Associationstatus.AsQueryable();
                if (!string.IsNullOrEmpty(parameter.Assname))
                {
                    query = query.Where(t => t.Assname.Contains(parameter.Assname));
                }

                result.Total = query.Count();
                result.Rows = query.OrderBy(t => t.AssociationId)
                    .Skip(parameter.Skip)
                    .Take(parameter.PageSize)
                    .ToList();
                return result;
            }
        }
        public static bool DeleteById(int id)
        {
            using (StudentContext db = new StudentContext())
            {
                var entity = db.Association.Find(id);
                if (null != entity)
                {
                    db.Association.Remove(entity);
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public static bool BatchDelete(string[] ids)
        {
            using (StudentContext db = new StudentContext())
            {
                var list = db.Association.Where(t => ids.Contains(t.AssociationId.ToString()));
                db.Association.RemoveRange(list);
                db.SaveChanges();
                return true;
            }
        }

        public static bool AddAssociation(Association entity)
        {
            using (StudentContext db = new StudentContext())
            {
                db.Association.Add(entity);
                db.SaveChanges();
                return true;
            }
        }
        
        public static Associationstatus AddAssociationstatus(Associationstatus entity)
        {
            using (StudentContext db = new StudentContext())
            {
                db.Associationstatus.Add(entity);
                db.SaveChanges();
                return entity;
            }
        }
        public static Association GetAssociationById(int id)
        {
            using (StudentContext db = new StudentContext())
            {
                var res = db.Association.Find(id);
                return res;
            }
        }
        public static bool Update(Association entity)
        {
            using (StudentContext db = new StudentContext())
            {
                var dbEntity = db.Association.Find(entity.AssociationId);
                if (null == dbEntity)
                {
                    return false;
                }
                else
                {
                    dbEntity.AssociationId = entity.AssociationId;
                    dbEntity.Assname = entity.Assname;
                    dbEntity.Assaddress = entity.Assaddress;
                    dbEntity.Zdteacher = entity.Zdteacher;
                    dbEntity.Asschairman = entity.Asschairman;

                    db.SaveChanges();
                    return true;
                }
            }
        }
        
        public static bool Updatestatus(Associationstatus entity)
        {
            using (StudentContext db = new StudentContext())
            {
                var data = db.Associationstatus.First(t => t.AssociationId == entity.AssociationId);
                data.Status = entity.Status;

                db.SaveChanges();
                return true;
            }
        }


        /// <summary>
        /// 保存文档
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool Updatewenjian(Associationstatus entity) {
            using (StudentContext db= new StudentContext()) {
                var dbEntity = db.Associationstatus.Find(entity.AssociationId);
                dbEntity.Wendong = entity.Wendong;

                db.SaveChanges();
                return true;
            }
        }
        /// <summary>
        /// 上传视频
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool UpdateVideo(Associationstatus entity) {
            using (StudentContext db=new StudentContext()) {
                var dbEntity = db.Associationstatus.Find(entity.AssociationId);
                dbEntity.Video = entity.Video;

                db.SaveChanges();
                return true;
            }
        }
    }
}
